package ipdesk.model.report.channel.dao;


import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.ibatis.dao.client.DaoManager;


import ipdesk.model.report.channel.bean.ChannelInfoBean;

import ecg.dao.DaoService;

public class ChannelInfoDao
{
	private static ChannelInfoDao singleton;
	
	protected ChannelInfoDao(){}
	
	public static ChannelInfoDao getInstance()
	{
		if(singleton == null)
		{
			singleton = new ChannelInfoDao();
		}
		
		return singleton;
	}
	
	private DaoManager	manager	= DaoService.getInstance();
	private IChannelInfo channelInfo 	= (IChannelInfo)manager.getDao(IChannelInfo.class, "ip_desk");
	
	/*
	 * 입력받은 데이터를 DB에 insert 하는 메서드.
	 */
	public int channelInfoReg(ChannelInfoBean channelInfoBean) throws Exception
	{
		int lastPromoteSeq = 0;
		
		try{
			// 트랜잭션 시작
			manager.startTransaction();
			
			// 상담정보 insert.
			lastPromoteSeq = channelInfo.channelInfoReg(channelInfoBean);
			
			manager.commitTransaction();
		}finally{
			manager.endTransaction();
		}
		
		return lastPromoteSeq;
	}
	
	public void insertFileData(ChannelInfoBean channelInfoBean) throws Exception
	{
				
		try{
			manager.startTransaction();
			
			channelInfo.insertFileData(channelInfoBean);
			
			manager.commitTransaction();
		}finally{
			manager.endTransaction();
		}						
	}
	
	
	public int getDataTotalCount(ChannelInfoBean channelInfoBean) throws Exception
	{
		
		int totalDataCount = channelInfo.getDataTotalCount(channelInfoBean); 
		
		return totalDataCount;
	}
	
	public List<ChannelInfoBean> getPagingDataListForUser(Map map) throws Exception
	{
		
		List<ChannelInfoBean> pageDataList = channelInfo.getPagingDataListForUser(map);
		

		return pageDataList; 
	}
	
	public ChannelInfoBean getDetailData(int channelSeq) throws Exception
	{
		ChannelInfoBean channelInfoBean = channelInfo.getDetailData(channelSeq);
		
		channelInfoBean.setAttendCompany(channelInfoBean.getAttendCompany().replace("<br/>", "\n"));
		channelInfoBean.setDiscussIssue(channelInfoBean.getDiscussIssue().replace("<br/>", "\n"));
		channelInfoBean.setFuturePlan(channelInfoBean.getFuturePlan().replace("<br/>", "\n"));
		if(channelInfoBean.getAttendSubject() != null){
			channelInfoBean.setAttendSubject(channelInfoBean.getAttendSubject().replace("<br/>", "\n"));
		}
		//System.out.println(" =---- :: "+seminarInfoBean.getStartDate());
		return channelInfoBean;
	}
	
	public void channelInfoUpdate(ChannelInfoBean channelInfoBean) throws Exception
	{
		try{
			manager.startTransaction();
			
			if(channelInfoBean.getUpload() != null){
			// 1. file DB insert
				channelInfo.insertFileData2(channelInfoBean);
			}
			// 2. seminar DB update
			channelInfo.channelInfoUpdate(channelInfoBean);
			
			manager.commitTransaction();
		}finally{
			manager.endTransaction();
		}
	}
	
	public void delFileData(String fileSeq) throws Exception
	{
		channelInfo.delFileData(fileSeq);
	}
	
	public void delDetailData(int channelSeq) throws Exception
	{
		channelInfo.delDetailData(channelSeq);
	}
}
